﻿using Biz.Buffer;
using Biz.EF.Platform.SG;
using Biz.Process.Entity;
using Hjt212Parser;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Biz.Process
{
    public static class Processor2031ToOneTable
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(Processor2031ToOneTable));
        public static void Process(string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrWhiteSpace(mn)) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue) return;
                var port = CommonInformation.GetLetportByMn(mn);
                if (port == null) return;

                using (var pCtx = new PlatformXpfContext())
                {                   
                    var codes = Parser.GetPollutantCodes(content);
                    if (codes == null) return;
                    Master master = new Master();
                    master.enterpriseId = port.企业基本信息ID;
                    master.letportId = port.ID;
                    DateTime time = datatime.Value;
                    master.monitorTime = time;
                    //todo:主污染物可以在此增加
                    if (codes.Contains("B01")||codes.Contains("w00000"))
                    {
                        master.contamTypeId = 2;
                        var rtd = Parser.GetDoubleByKey("B01-Cou", content);
                        if (!rtd.HasValue) Parser.GetDoubleByKey("w00000-Cou", content);
                        master.sum = rtd ?? 0;
                    }
                    else if (codes.Contains("B02")|| codes.Contains("a00000"))
                    {
                        master.contamTypeId = 1;
                        var rtd = Parser.GetDoubleByKey("B02-Cou", content);
                        if (!rtd.HasValue) Parser.GetDoubleByKey("a00000-Cou", content);
                        master.sum = rtd ?? 0;
                    }
                    master.rtd = 0;
                    var begin = new DateTime(time.Year, time.Month, time.Day, 0, 0, 0);
                    var end = begin.AddDays(1);
                
                    var masterRecord = pCtx
                          .主日数据
                          .Where(t => t.企业基本信息ID == port.企业基本信息ID)
                          .Where(t => t.企业排口ID == port.ID)
                          .Where(t => t.监测时间 >= begin)
                          .FirstOrDefault(t => t.监测时间 < end);
                    if (masterRecord == null)
                    {
                        masterRecord = new 主日数据();
                        masterRecord.企业基本信息ID = port.企业基本信息ID;
                        masterRecord.企业排口ID = master.letportId;
                        pCtx.主日数据.Add(masterRecord);
                    }
                    masterRecord.监测时间 = master.monitorTime;
                    masterRecord.污染物类别ID = master.contamTypeId;
                    masterRecord.时段量 = master.sum;
                    pCtx.SaveChanges();

                    Helper.RemoveMasterFactor(codes, true);

                    Slave slave = new Slave();
                    slave.masterId = masterRecord.ID;

                    foreach (var code in codes)
                    {
                        var contam = CommonInformation.GetContamCode(code);
                        if (contam == null) continue;
                        slave.contamId = contam.ID;
                        var cou = Parser.GetDoubleByKey(code + "-Cou", content);
                        slave.cou = cou ?? 0;
                        slave.zsCou = Parser.GetDoubleByKey(code + "-ZsCou", content);
                        slave.min = Parser.GetDoubleByKey(code + "-Min", content);
                        slave.zsMin = Parser.GetDoubleByKey(code + "-ZsMin", content);
                        slave.avg = Parser.GetDoubleByKey(code + "-Avg", content);
                        slave.zsAvg = Parser.GetDoubleByKey(code + "-ZsAvg", content);
                        slave.max = Parser.GetDoubleByKey(code + "-Max", content);
                        slave.zsMax = Parser.GetDoubleByKey(code + "-ZsMax", content);

                        var slaveRecord = pCtx
                            .从日数据
                            .Where(t => t.主日数据ID == slave.masterId)
                            .FirstOrDefault(t => t.污染物编码ID == slave.contamId);
                        if (slaveRecord == null)
                        {
                            slaveRecord=new 从日数据();
                            slaveRecord.主日数据ID = slave.masterId;
                            slaveRecord.污染物编码ID = slave.contamId;
                           
                            pCtx.从日数据.Add(slaveRecord);
                        }
                        slaveRecord.Cou = slave.cou;
                        slaveRecord.ZSCou = slave.zsCou;
                        slaveRecord.Avg = slave.avg;
                        slaveRecord.ZSAvg = slave.zsAvg;
                        slaveRecord.Min = slave.min;
                        slaveRecord.ZSMin = slave.zsMin;
                        slaveRecord.Max = slave.max;
                        slaveRecord.ZSMax = slave.zsMax;
                        pCtx.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {                
                Log.Error(ex);
                Log.Error(content);
            }
        }
    }
}
